package com.gpframework.module.purchase.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.extra.qrcode.QrCodeUtil;
import com.gpframework.common.config.GpframeworkConfig;
import com.gpframework.common.constant.Constants;
import com.gpframework.common.utils.DateUtils;
import com.gpframework.common.utils.StringUtils;
import com.gpframework.common.utils.uuid.IdUtils;
import com.gpframework.module.purchase.domain.GpProcureScheduling;
import com.gpframework.module.purchase.service.IGpProcureSchedulingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gpframework.module.purchase.mapper.GpPurchaseReceiptMapper;
import com.gpframework.module.purchase.domain.GpPurchaseReceipt;
import com.gpframework.module.purchase.domain.vo.GpPurchaseReceiptVo;
import com.gpframework.module.purchase.service.IGpPurchaseReceiptService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.Date;
import java.util.List;
/**
 * @ClassName GpPurchaseReceipt
 * @Author gpframework
 * @Date 2023-12-22
 * @Version 1.0.0
 * @Description 采购收货单Service业务层处理
 */
@Service
public class GpPurchaseReceiptServiceImpl extends ServiceImpl<GpPurchaseReceiptMapper, GpPurchaseReceipt> implements IGpPurchaseReceiptService {

    @Autowired
    private IGpProcureSchedulingService iGpProcureSchedulingService;

    /**
     * @Description 查询列表返回VO 用于返回给前端的列表接口
     * @Author gpframework
     * @Date 2023-12-22
     * @Param [lqw]
     * @Return List<GpPurchaseReceiptVo>
     */
    @Override
    public List<GpPurchaseReceiptVo> listVo(QueryWrapper<GpPurchaseReceipt> lqw) {
        return this.baseMapper.listVo(lqw);
    }

    /**
     * @Description 通过查询详情VO 用于返回给前端的列详情接口
     * @Author gpframework
     * @Date 2023-12-22
     * @Param [id]
     * @Return GpPurchaseReceiptVo
     */
    @Override
    public GpPurchaseReceiptVo getVoById(Long id) {
        return this.baseMapper.getVo(id);
    }

    /**
     * 生成采购到货单
     * @param ids
     * @param createBy
     * @return
     */
    @Override
    @Transactional
    public boolean createPurchaseReceipt(List<Long> ids, String createBy) {
        GpProcureScheduling gpProcureScheduling;
        GpPurchaseReceipt gpPurchaseReceipt;
        Date now = new Date();
        for (Long id : ids) {
            gpProcureScheduling = iGpProcureSchedulingService.getById(id);
            if (gpProcureScheduling == null) {
                continue;
            }
            gpPurchaseReceipt = Convert.convert(new TypeReference<GpPurchaseReceipt>() {}, gpProcureScheduling);
            if (StringUtils.isNotBlank(createBy)) {
                gpPurchaseReceipt.setCreateBy(createBy);
            }
            gpPurchaseReceipt.setSendTime(now);
            gpPurchaseReceipt.setProductionApplicationId(gpProcureScheduling.getProductionApplicationId());
            gpPurchaseReceipt.setPurchasingDetailsId(gpProcureScheduling.getPurchasingDetailsId());
            gpPurchaseReceipt.setProcureSchedulingId(gpProcureScheduling.getId());
            gpPurchaseReceipt.setRelatedOrderNumber(gpProcureScheduling.getRelatedNeedNumber());
            boolean save = this.save(gpPurchaseReceipt);
            if (save) {
                // 生成采购到货二维码
                gpPurchaseReceipt.setOrderQrCode(this.getQrCode(gpPurchaseReceipt.getId()));
                this.updateById(gpPurchaseReceipt);
            }
        }
        return true;
    }

    /**
     * 生成采购跟踪二维码
     * @param id 采购跟踪id
     * @return
     */
    private String getQrCode(Long id) {
        if (id == null) {
            return null;
        }
        String date = DateUtils.datePath();
        String uuid = IdUtils.fastUuid();
        String filePath = GpframeworkConfig.getUploadPath() + "/" + date + "/" + uuid + ".jpg";
        File file = FileUtil.file(filePath);
        if (!file.exists()) {
            // 文件不存在，创建新文件
            FileUtil.touch(file);
        }
        QrCodeUtil.generate("id=" + id + "&type=1", 300, 300, file);
        return Constants.RESOURCE_PREFIX + "/upload/" + date + "/" + uuid + ".jpg";
    }

    @Override
    public boolean copy(Long id, Integer copyNum) {
        GpPurchaseReceipt gpPurchaseReceipt = this.getById(id);
        if (gpPurchaseReceipt == null) {
            return false;
        }
        for (int i = 0; i < copyNum; i++) {
            gpPurchaseReceipt.setId(null);
            this.save(gpPurchaseReceipt);
        }
        return true;
    }
}
